<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="home">
{
  style: {
    navigationBarTitleText: '四川富临',
    navigationBarTextStyle: '#0000FF',
    navigationStyle: 'custom',
    navigationBarBackgroundColor: '#a8dced',
  },
}
</route>
<template>
  <view class="w-[100vw] h-[100vh] p-[10px] box-border content">
    <image class="logo" src="/static/logo.png"></image>
    <view class="text-area">
      <text class="title">{{ title }}</text>
    </view>
  </view>
</template>

<script setup>
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import { useSysStore } from '@/store/systemInfo'
import dd from 'dingtalk-jsapi' // 引入钉钉api
import { userLogin, getUserInfo, getUserInfoAll, dingLogin } from '@/api/user'

defineOptions({
  name: 'Home',
})

const title = ref('欢迎进入升华HRMS！')
const sysStore = useSysStore()

onLoad((options) => {
  initFnc(options)
})

// 初始化
const initFnc = (options) => {
  // 小程序通过读取getSystemInfo中的platform来判断是否鸿蒙系统。
  const systemInfo = uni.getSystemInfoSync()
  const isHarmony = systemInfo.platform === 'OpenHarmony'
  if (isHarmony) {
    uni.redirectTo({
      url: '/pages/login/index',
    })
    return
  }
  const corpId = sysStore.corpId
  dd.ready(() => {
    dd.runtime.permission.requestAuthCode({
      corpId: corpId,
      onSuccess: async function (info) {
        const res = await dingLogin({ code: info.authCode || info.code })
        if (res.data.code == 200) {
          sysStore.setToken(res.data.data.token)
          sysStore.setUserInfo(res.data.data.user)
          sysStore.setPermissions(res.data.data.permissions)
          if (options.code && options.code == '1') {
            uni.redirectTo({
              url: '/pages/mine/basicInformation/index',
            })
          } else {
            uni.switchTab({
              url: '/pages/home/index',
            })
          }
        } else {
          uni.showToast({
            title: res.errmsg,
            icon: 'fail',
            duration: 5000,
          })
          uni.redirectTo({
            url: '/pages/login/index',
          })
          return
        }
      },
      onFail: function (err) {
        console.log(err, 888)
      },
    })
  })
}
</script>

<style lang="scss" scoped>
.textBox {
  padding: 10px;
  width: 100vw;
  box-sizing: border-box;
}
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 200rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
  width: 100%;
}
</style>
